package com.example.admin.controller.question;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.question.entity.QuestionCategory;
import com.example.question.entity.Result;
import com.example.question.service.QuestionCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 题目分类控制器
 * @author 颜殿
 */
@RestController
@RequestMapping("/api/questionCategory")
public class QuestionCategoryController {

    @Autowired
    private QuestionCategoryService categoryService;

    /**
     * 添加题目分类
     * @param category 题目分类实体
     * @return 操作结果
     */
    @PostMapping
    public Result<Boolean> addCategory(@RequestBody QuestionCategory category) {
        return Result.success(categoryService.addCategory(category));
    }

    /**
     * 删除题目分类
     * @param id 要删除的题目分类ID
     * @return 操作结果
     */
    @DeleteMapping("/{id}")
    public Result<Boolean> deleteCategory(@PathVariable Integer id) {
        return Result.success(categoryService.deleteCategory(id));
    }

    /**
     * 修改题目分类
     * @param category 题目分类实体
     * @return 操作结果
     */
    @PutMapping
    public Result<Boolean> updateCategory(@RequestBody QuestionCategory category) {
        return Result.success(categoryService.updateCategory(category));
    }

    /**
     * 查询题目分类的题目数量
     * 获取题目分类的分页列表
     * @param name 题目分类的名称
     * @return 返回结果
     */
    @GetMapping("/category/count")
    public Long countQuestionsByCategory(@RequestParam(required = false) String name) {
        return categoryService.countQuestionsByCategory(name);
    }

    /**
     * 分页查询题目分类
     * @param pageNum 页码
     * @param pageSize 条数
     * @return 返回数据
     */
    @GetMapping("/page")
    public Result<Page<QuestionCategory>> pageCategories(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "5") Integer pageSize) {
        return Result.success(categoryService.pageCategories(pageNum, pageSize));
    }

    /**
     * 获取所有题目分类
     * @return 分类列表
     */
    @GetMapping("/list")
    public Result<List<QuestionCategory>> listAllCategories() {
        return Result.success(categoryService.listAllCategories());
    }
} 